// pages/submit/submit.js
Page({
  data: {
    imgList:[],
    tempFilePaths:[],
    clue:'',
    taskId:'',
    volunteerId:-1,
    lostmanID:'',
    lostmanName:'',
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var taskId_get = wx.getStorageSync('taskId');//wx.getStorageSync(key)，获取本地缓存
    var lostmanName_get = wx.getStorageSync('lostmanName');//wx.getStorageSync(key)，获取本地缓存
    var familyName_get = wx.getStorageSync('familyName');//wx.getStorageSync(key)，获取本地缓存
    var lostmanID_get = wx.getStorageSync('lostmanId');//wx.getStorageSync(key)，获取本地缓存
    this.setData({
      familyName:familyName_get,
      taskId:taskId_get,
      lostmanName:lostmanName_get,
      lostmanID:lostmanID_get
    })
    console.log('老人的taskid为：',this.data.taskId)
    console.log('老人的lostmanID为：',this.data.lostmanID)
  },
  /**
   * 信息补充内容
   */
  lostmanRemark_put:function(e){
    var lostmanRemark_get = e.detail.value;
    if(lostmanRemark_get.length != 0){
      this.setData({
        clue:lostmanRemark_get
      })
    }
    console.log("获取到的老人描述为：",this.data.clue)
  },
  gotoShow:function(){ // 选择或拍摄图片
    wx.chooseImage({
      count: 9, //设置选择照片数量
      success:res=>{
        this.setData({
          tempFilePaths:this.data.tempFilePaths.concat(res.tempFilePaths) //图片的本地文件路径列表
        })
        console.log("图片选择成功，链接为：",res.tempFilePaths);
        console.log("保存的tempFilePaths为：",this.data.tempFilePaths);
        wx.showLoading({
          title: '上传中…',
        })
        //循环上传图片到云存储
        for(var i =0 ; i < res.tempFilePaths.length; i++){
          var newImagePath = 'test/' + Math.floor(Math.random()*1000000) + '.png'
          wx.cloud.uploadFile({
            cloudPath:newImagePath,
            filePath:res.tempFilePaths[i],
            success:res=>{
              console.log("成功放置云存储中",res.fileID);
              this.setData({
                imgList:this.data.imgList.concat(res.fileID)
              })
            }
          })
        }
        console.log("图片列表",this.data.imgList);
        wx.hideLoading()
        wx.showToast({
          title: '图片上传成功',
        })
      }
    })
  },
  /**
   * 预览上传的图片
   */
  topic_preview:function(event){
    console.log("点击的图片下标",event.currentTarget.id)
    var that = this
    //图片预览
    wx.previewImage({
      current: that.data.imgList[event.currentTarget.id], // 当前显示图片的http链接
      urls: that.data.imgList // 需要预览的图片http链接列表
    })
  },
  /**
   * 长按删除图片
   */
  deleteImage: function (e) {
    var that = this;
    var imgList = that.data.imgList;
    var index = e.target.id;//获取当前长按图片下标
    console.log("长按的图片下标",index);
    wx.showModal({
      title: '提示',
      content: '确定要删除此图片吗？',
      success: function (res) {
      if (res.confirm) {
        console.log('确定删除');
        // 从云存储中删除
        wx.cloud.deleteFile({
          fileList:[imgList[index]],
          success(res){
            console.log('该文件已经成功从云存储中删除',res)
          },
          fail(err){
            console.log("文件从云存储中删除发生错误",err)
          }
        })
        imgList.splice(index, 1);
      } else if (res.cancel) {
        console.log('取消删除');
        return false;    
       }
      that.setData({
        imgList
      });
      console.log("删除后图片列表：",imgList)
     }
    })
   },
  /**
   * 表单提交
   */
  submitBtn:function(){
    wx.showModal({
      title: '报案表单提交',      
      content: '确定信息无误进行提交？',
      success:(res)=>{
        if(res.cancel){
          // 点击取消
        }else{
          // 点击确定，进行表单提交
          var data = {
            clue:this.data.clue,
            taskId:this.data.taskId,
            volunteerId:-1
          }
          console.log("提交到服务器的表单数据为：",data)
          wx.request({
            url: 'https://www.lanotherl.com/task/clue',
            data,
            method:"POST",
            // Post请求
            header: {
              "content-type": "application/x-www-form-urlencoded"
            },
            success:(res)=>{
              console.log("提交到服务器数据成功：",res)
              Promise.all(this.data.tempFilePaths.map(picture=>{ //上传图片到服务器
                wx.uploadFile({
                  filePath: picture,
                  name: 'file',
                  url: 'https://www.lanotherl.com/upload/family/face',
                  formData:{
                    lostmanId:this.data.lostmanID
                  },
                  success:(res)=>{
                    console.log('图片上传到服务器成功',res)
                  },
                  fail:(res)=>{
                    console.log('图片上传到服务器失败',res)
                  }
                })
              }))
            }
          })
        }
      }
    })
  }
})